package org.nimi317.web_gis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nimi317.web_gis.context.SecurityContextHolder;
import org.nimi317.web_gis.dao.UpdateMapper;
import org.nimi317.web_gis.entity.Update;
import org.nimi317.web_gis.service.IUpdateService;
import org.nimi317.web_gis.utils.TimeUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author thunderobot
 */
@Service
public class IUpdateServiceImpl extends ServiceImpl<UpdateMapper, Update> implements IUpdateService {

    @Override
    public boolean isUpdated() {
        LambdaQueryWrapper<Update> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Update::getTimestamp, TimeUtils.getCurrentUsefulDateTime());
        return this.exists(wrapper);
    }

    @Override
    public LocalDateTime getLastUpdateTime() {
        return this.getById(1).getTimestamp();
    }

    @Override
    public void updateTime() {
        Update update = new Update();
        update.setId(1);
        LocalDateTime time = SecurityContextHolder.getLastTime();
        update.setTimestamp(time);
        this.saveOrUpdate(update);
    }
}
